home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / prog / strpp212.zip / DEMO.CPP next >
C/C++ Source or Header  |  1993-02-17  |  11KB  |  315 lines

  1. /* -------------------------------------------------------------------- */
  2. /* String++ Version 2.12                          Last revised 02/16/93 */
  3. /*                                                                      */
  4. /* Enhanced string class for Turbo C++/Borland C++.                     */
  5. /* Copyright 1991-1993 by Carl W. Moreland                              */
  6. /*                                                                      */
  7. /* demo.cpp                                                             */
  8. /* -------------------------------------------------------------------- */
  9. /* Demonstration of String++ methods.                                   */
  10. /* -------------------------------------------------------------------- */
  11.  
  12. #include <stdio.h>
  13. #include <conio.h>
  14. #include <stdlib.h>
  15.  
  16. #if defined (__ZTC__)
  17. #include <disp.h>
  18. #define _NOCURSOR DISP_NONDISPLAY
  19. #define _NORMALCURSOR DISP_CURSORHALF
  20. #define _setcursortype(k) disp_setcursortype(k)
  21. #define clrscr() disp_eeop()
  22. #endif
  23.  
  24. #include "str.h"
  25.  
  26. void pause(void);
  27.  
  28. void intro()
  29. {
  30.   clrscr();
  31.   _setcursortype(_NOCURSOR);
  32.  
  33.   String title1 = "String++ Version 2.12";
  34.   String title2 = "Written by Carl W. Moreland";
  35.   String title3 = "Demonstration of methods & operators";
  36.   String title4 = "Hit any key to continue,";
  37.   String title5 = "or <Ctrl>C to exit...";
  38.  
  39.   cout << "\n\n\n\n\n\n\n\n";
  40.   cout << justify(title1, CENTER_JUSTIFY, 78) << "\n";
  41.   cout << justify(title2, CENTER_JUSTIFY, 78) << "\n\n";
  42.   cout << justify(title3, CENTER_JUSTIFY, 78) << "\n\n";
  43.   cout << justify(title4, CENTER_JUSTIFY, 78) << "\n";
  44.   cout << justify(title5, CENTER_JUSTIFY, 78) << "\n";
  45.  
  46.   pause();
  47. }
  48.  
  49. void test1()
  50. {
  51.   cout << "Create a string and assign it \"Hello World.\":\n";
  52.   string str1 = "Hello World.";
  53.   cout << "string str1 = \"Hello World.\";\n\n";
  54.  
  55.   cout << "The string contents are returned by the () operator:\n";
  56.   printf("printf\(\"%%s\\n\", str1\(\)\)\; ---> %s\n\n", str1());
  57.   cout << "or the cast operator:\n";
  58.   printf("printf\(\"%%s\\n\", str1\)\; ---> %s\n\n", str1);
  59.  
  60.   cout << "Now assign str1 to a second string:\n";
  61.   string str2 = str1;
  62.   cout << "string str2 = str1;\n";
  63.   cout << "str2() = " << str2 << "\n\n";
  64.  
  65.   cout << "We can also assign numeric values to strings:\n";
  66.   str2 = 1024;
  67.   cout << "str2 = 1024;\n";
  68.   cout << "str2() = " << str2 << "\n\n";
  69.  
  70.   cout << "Create multiple characters by passing an optional multiplier to\n";
  71.   cout << "  the string constructor:\n\n";
  72.   str2 = "/* " + string('-', 40) + " */";
  73.   cout << "str2 = \"/* \" + string(\'-\', 40) + \" */\";\n";
  74.   cout << "str2() = " << str2() << "\n\n";
  75.  
  76.   pause();
  77.  
  78.   cout << "Placing a number in the () operator returns the substring\n";
  79.   cout << "  of the string starting at that number:\n";
  80.   cout << "str1(6) = " << str1(6) << "\n\n";
  81.  
  82.   cout << "Placing a second number in the () operator limits the substring\n";
  83.   cout << "  to that many characters:\n";
  84.   cout << "str1(6,2) = " << str1(6,2) << "\n\n";
  85.  
  86.   cout << "The nth character is returned by the [] operator:\n";
  87.   cout << "str1[6] = " << str1[6] << "\n\n";
  88.  
  89.   cout << "The [] operator can also be used to replace the nth character:\n";
  90.   str1[6] = 'w';
  91.   cout << "str1[6] = 'w';\n";
  92.   cout << "str1() = " << str1 << "\n\n";
  93.  
  94.   cout << "The length of str1 is given by the Length() member function:\n";
  95.   cout << "str1.Length() = " << str1.Length() << "\n\n";
  96.  
  97.   pause();
  98.  
  99.   cout << "The == operator will work for string == string, string == char*,\n";
  100.   cout << "  or for char* == string:\n\n";
  101.   if(str1 == "Hello world.")
  102.     cout << "str1 == \"Hello world.\"\n";
  103.   else
  104.     cout << "str1 != \"Hello world.\"\n";
  105.  
  106.   if("Hello world." == str1)
  107.     cout << "\"Hello world.\" == str1\n";
  108.   else
  109.     cout << "\"Hello world.\" != str1\n";
  110.  
  111.   pause();
  112.  
  113.   cout << "Let's create a string that's equal to str1*2:\n\n";
  114.   str2 = str1*2;
  115.   cout << "str2 = str1*2;\n";
  116.   cout << "str2() = " << str2() << "\n\n";
  117.  
  118.   cout << "Now multiply str2 by 2:\n\n";
  119.   str2 *= 2;
  120.   cout << "str2 *= 2;\n";
  121.   cout << "str2() = " << str2() << "\n\n";
  122.  
  123.   pause();
  124.  
  125.   cout << "The C-style function toupper() will return the upper case version\n";
  126.   cout << "  of a string without changing the string itself, whereas the\n";
  127.   cout << "  member function toUpper() will convert the string internally:\n\n";
  128.   cout << "str1() = " << str1 << "\n";
  129.   cout << "toupper(str1) = " << toupper(str1) << "\n";
  130.   cout << "str1() = " << str1 << "\n";
  131.   str1.toUpper();
  132.   cout << "str1.toUpper();\n";
  133.   cout << "str1() = " << str1 << "\n\n";
  134.  
  135.   pause();
  136. }
  137.  
  138. void test2()
  139. {
  140.   cout << "Create a new string with the contents \"only\":\n\n";
  141.   string str3 = "only";
  142.   cout << "string str3 = \"only\";\n\n";
  143.  
  144.   cout << "Now use the + operator to add to it:\n\n";
  145.   str3 = "This is " + str3 + " a test";
  146.   cout << "str3 = \"This is \" + str3 + \" a test\";\n";
  147.   cout << "str3() = " << str3() << "\n\n";
  148.  
  149.   cout << "The Delete() member function can be used to delete a substring:\n\n";
  150.   str3.Delete(8, 5);
  151.   cout << "str3.Delete(8, 5);\n";
  152.   cout << "str3() = " << str3() << "\n\n";
  153.  
  154.   cout << "The Insert() member function can be used to insert a substring:\n\n";
  155.   str3.Insert(8, "still ");
  156.   cout << "str3.Insert(8, \"still \");\n";
  157.   cout << "str3() = " << str3() << "\n\n";
  158.  
  159.   pause();
  160.  
  161.   cout << "Create a new string and use the += operator to append to it:\n\n";
  162.   string str4 = "Please ";
  163.   str4 += "stand by...";
  164.   cout << "string str4 = \"Please \";\n";
  165.   cout << "str4 += \"stand by...\";\n";
  166.   cout << "str4() = " << str4() << "\n\n";
  167.  
  168.   cout << "Use the left(), mid(), & right() functions to return substrings:\n\n";
  169.   cout << " left(str4, 6)    = " << left(str4, 6)   << "\n";
  170.   cout << "  mid(str4, 7, 5) = " << mid(str4, 7, 5) << "\n";
  171.   cout << "right(str4, 5)    = " << right(str4, 5)  << "\n\n";
  172.  
  173.   pause();
  174.  
  175.   cout << "The justify() function will expand a string to a total width of n\n";
  176.   cout << "  by adding blanks and justify the non-blank portion:\n\n";
  177.   string str5 = "Hello world.";
  178.   string str5a = "│" + justify(str5, LEFT, 20)   + "│";
  179.   string str5b = "│" + justify(str5, CENTER, 20) + "│";
  180.   string str5c = "│" + justify(str5, RIGHT, 20)  + "│";
  181.  
  182.   cout << "string str5 = str1;\n";
  183.   cout << "str5a = \"│\" + justify(str5, LEFT, 20)   + \"│\";\n";
  184.   cout << "str5b = \"│\" + justify(str5, CENTER, 20) + \"│\";\n";
  185.   cout << "str5c = \"│\" + justify(str5, RIGHT, 20)  + \"│\";\n";
  186.   cout << "str5a() = " << str5a() << "\n";
  187.   cout << "str5b() = " << str5b() << "\n";
  188.   cout << "str5c() = " << str5c() << "\n";
  189.  
  190.   cout << "\nHere's what happens when clipping is used:\n\n";
  191.   str5a = "│" + justify(str5, LEFT, 8, CLIP)   + "│";
  192.   str5b = "│" + justify(str5, CENTER, 8, CLIP) + "│";
  193.   str5c = "│" + justify(str5, RIGHT, 8, CLIP)  + "│";
  194.  
  195.   cout << "str5a = \"│\" + justify(str5, LEFT, 8, CLIP)   + \"│\";\n";
  196.   cout << "str5b = \"│\" + justify(str5, CENTER, 8, CLIP) + \"│\";\n";
  197.   cout << "str5c = \"│\" + justify(str5, RIGHT, 8, CLIP)  + \"│\";\n";
  198.   cout << "str5a() = " << str5a() << "\n";
  199.   cout << "str5b() = " << str5b() << "\n";
  200.   cout << "str5c() = " << str5c() << "\n";
  201.  
  202.   pause();
  203.  
  204.   cout << "The trim() function will remove leading and trailing whitespace:\n\n";
  205.   string str6 = "\t\t  " + str5 + " \t ";
  206.   cout << "string str6 = \"\\t\\t  \" + str5 + \" \\t \";\n\n";
  207.  
  208.   string str6a = trim(str6, LEFT);
  209.   cout << "str6a = trim(str6, LEFT);\n";
  210.   cout << "str6a() = │" << str6a() << "│\n\n";
  211.   string str6b = trim(str6, RIGHT);
  212.   cout << "str6b = trim(str6, RIGHT);\n";
  213.   cout << "str6b() = │" << str6b() << "│\n\n";
  214.   string str6c = trim(str6);
  215.   cout << "str6c = trim(str6);\n";
  216.   cout << "str6c() = │" << str6c() << "│\n\n";
  217.  
  218.   cout << "You can also specify the character to be trimmed:\n\n";
  219.   str6 = "Here we go again..........";
  220.   cout << "str6 = " << str6 << "\n";
  221.   str6.Trim(RIGHT, '.');
  222.   cout << "str6.Trim(RIGHT, '.');\n";
  223.   cout << "str6 = " << str6 << "\n";
  224.  
  225.   pause();
  226. }
  227.  
  228. void test3()
  229. {
  230.   int i, pos, num;
  231.   string *array;
  232.   string str1 = "HELLO WORLD.";
  233.   string str3 = "This is only a test";
  234.   string str4 = "Please stand by...";
  235.  
  236.   cout << "The following are AWK functions.\n\n";
  237.  
  238.   cout << "index() returns the location of a substring in a string:\n\n";
  239.   pos = index(str1, "WOR");
  240.   cout << "str1() = " << str1 << "\n";
  241.   cout << "pos = index(str1, \"WOR\";\n";
  242.   cout << "pos = " << pos << "\n";
  243.  
  244.   pause();
  245.  
  246.   cout << "split() will split a string at a given substring delimiter:\n\n";
  247.   string str7 = "d:\\prog\\turboc\\str";
  248.   num = split(str7, array, "\\");
  249.  
  250.   cout << "string str7 = \"d:\\prog\\turboc\\str\";\n";
  251.   cout << "num = split(str7, array, \"\\\";\n\n";
  252.  
  253.   cout << "In this example, str7 is split using the \\ character as the\n";
  254.   cout << "  delimiter. The results are placed in array, which now contains:\n\n";
  255.   for(i=0; i<num; i++)
  256.     cout << "array[" << i << "] = " << array[i] << "\n";
  257.  
  258.   pause();
  259.  
  260.   cout << "gsub() performs a global substitution. In this example, we want to\n";
  261.   cout << "  replace all \\'s with /'s:\n\n";
  262.   cout << "str7() = " << str7 << "\n";
  263.   i = gsub("\\", "/", str7);
  264.   cout << "i = gsub(\"\\\", \"/\", str7);\n";
  265.   cout << "str7() = " << str7 << "\n";
  266.   cout << "i = " << i << "\n";
  267.  
  268.   pause();
  269.  
  270.   cout << "sub() performs a one-time substitution:\n\n";
  271.   cout << "str1() = " << str1 << "\n";
  272.   i = sub("LO", "P ME,", str1);
  273.   cout << "i = sub(\"LO\", \"P ME,\", str1);\n";
  274.   cout << "str1() = " << str1 << "\n";
  275.   cout << "i = " << i << "\n";
  276.  
  277.   pause();
  278.  
  279.   cout << "substr() returns a substring of the string starting at n. If a\n";
  280.   cout << "  a third parameter is given, it represents the maximum number\n";
  281.   cout << "  of characters to return\n\n";
  282.   string str_3 = substr(str3, 8);
  283.   string str_4 = substr(str4, 7, 5);
  284.   cout << "str3() = " << str3 << "\n";
  285.   cout << "substr(str3, 8) = " << str_3 << "\n\n";
  286.   cout << "str4() = " << str4 << "\n";
  287.   cout << "substr(str4, 7, 5) = " << str_4 << "\n\n";
  288.  
  289.   pause();
  290. }
  291.  
  292. main()
  293. {
  294.   intro();
  295.   test1();
  296.   test2();
  297.   test3();
  298.   _setcursortype(_NORMALCURSOR);
  299.   return 0;
  300. }
  301.  
  302. void pause(void)
  303. {
  304.   char ch = getch();
  305.   if(!ch)
  306.     getch();
  307.   if(ch == 0x03)
  308.   {
  309.     _setcursortype(_NORMALCURSOR);
  310.     exit(0);
  311.   }
  312.   clrscr();
  313.   cout << "\n";
  314. }
  315.